package com.leoren.run.activity.main;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.leoren.run.MainActivity;
import com.leoren.run.R;
import com.leoren.run.activity.myself.UpdateEmailActivity;
import com.leoren.run.activity.myself.UpdatePasswordActivity;
import com.leoren.run.activity.myself.UpdatePhoneActivity;
import com.leoren.run.activity.myself.UpdateUsernameActivity;
import com.leoren.run.activity.run.RunRecordActivity;
import com.leoren.run.common.Const;
import com.leoren.run.dbbean.UserDB;
import com.leoren.run.log.L;
import com.leoren.run.utils.ToastUtil;

import org.litepal.LitePal;


/**
 * 个人主页Fragment
 */
public class MyFragment extends Fragment implements View.OnClickListener {

    private View rootView;

    private Toolbar toolbar;

    private ImageView faceImgView;

    private LinearLayout updateUsernameLayout;
    private TextView usernameText;
    private ImageView updateUsernameBtn;

    private ImageView sexImg;

    private LinearLayout updatePhoneLayout;
    private TextView phoneText;

    private LinearLayout updateEmailLayout;
    private TextView emailText;

    private RelativeLayout updatePasswordLayout;
    private TextView updatePasswordText;
    private ImageView updatePasswordBtn;

    private RelativeLayout myRecordLayout;
    private TextView myRecordText;
    private ImageView recordBtn;

    private Button logoutBtn;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_my, container, false);

        initViews();
        initData();

        return rootView;
    }

    /**
     * 初始化View
     */
    private void initViews(){

        toolbar = rootView.findViewById(R.id.my_toolbar);
        toolbar.setTitle("我的");

        faceImgView = rootView.findViewById(R.id.update_face_img_btn);

        updateUsernameLayout = rootView.findViewById(R.id.update_username_layout);
        usernameText = rootView.findViewById(R.id.my_username_text);
        updateUsernameBtn = rootView.findViewById(R.id.update_username_btn);

        updatePhoneLayout = rootView.findViewById(R.id.update_phone_layout);
        phoneText = rootView.findViewById(R.id.update_phone_text);

        updatePasswordLayout = rootView.findViewById(R.id.update_password_layout);
        updatePasswordText = rootView.findViewById(R.id.update_password_text);
        updatePasswordBtn = rootView.findViewById(R.id.update_password_btn);

        updateEmailLayout = rootView.findViewById(R.id.update_email_layout);
        emailText = rootView.findViewById(R.id.update_email_text);

        myRecordLayout = rootView.findViewById(R.id.my_run_record_layout);
        myRecordText = rootView.findViewById(R.id.my_run_record_text);
        recordBtn = rootView.findViewById(R.id.my_run_record_btn);

        logoutBtn = rootView.findViewById(R.id.logout_btn);
        sexImg = rootView.findViewById(R.id.my_sex_img);

        faceImgView.setOnClickListener(this);
        updateUsernameLayout.setOnClickListener(this);
        usernameText.setOnClickListener(this);
        updateUsernameBtn.setOnClickListener(this);

        updatePhoneLayout.setOnClickListener(this);
        phoneText.setOnClickListener(this);

        updatePasswordLayout.setOnClickListener(this);
        updatePasswordText.setOnClickListener(this);
        updatePasswordBtn.setOnClickListener(this);

        logoutBtn.setOnClickListener(this);

        emailText.setOnClickListener(this);
        updateEmailLayout.setOnClickListener(this);

        myRecordLayout.setOnClickListener(this);
        myRecordText.setOnClickListener(this);
        recordBtn.setOnClickListener(this);
    }

    /**
     * 初始化界面数据
     */
    private void initData(){
        UserDB userDB = LitePal.findFirst(UserDB.class);
        if(userDB == null){
            ToastUtil.MakeToast("网络出错，请退出重新登录");
            return;
        }
        L.e("MyFragment Resume" + userDB.toString());
        if(userDB.getFaceImage() == null || userDB.getFaceImage().equals("")){
            Glide.with(this.getActivity().getApplicationContext()).load(R.drawable.head_icon).into(faceImgView);
        }else {
            Glide.with(this.getActivity().getApplicationContext()).load(userDB.getFaceImage()).into(faceImgView);
        }
        usernameText.setText(userDB.getUsername());
        if(userDB.getSex() == Const.SexCode.MALE){
            Glide.with(this.getActivity().getApplicationContext()).load(R.drawable.men01).into(sexImg);
        }else {
            Glide.with(this.getActivity().getApplicationContext()).load(R.drawable.women01).into(sexImg);
        }
        phoneText.setText(userDB.getPhone());
        emailText.setText(userDB.getEmail());
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.update_face_img_btn:
                updateFaceImg();
                break;
            case R.id.update_username_layout:
            case R.id.my_username_text:
            case R.id.update_username_btn:
                updateUsername();
                break;
            case R.id.update_phone_layout:
            case R.id.update_phone_text:
            case R.id.update_phone_title:
                updatePhone();
                break;
            case R.id.update_password_layout:
            case R.id.update_password_btn:
            case R.id.update_password_text:
                updatePassword();
                break;
            case R.id.logout_btn:
                logout();
                break;
            case R.id.update_email_layout:
            case R.id.update_email_text:
            case R.id.update_email_title:
                updateEmail();
                break;
            case R.id.my_run_record_layout:
            case R.id.my_run_record_text:
            case R.id.my_run_record_btn:
                openRecord();
                break;
            default:
                break;
        }
    }

    /**
     * 打开个人跑步记录界面
     */
    private void openRecord(){
        Intent intent = new Intent(this.getActivity(), RunRecordActivity.class);
        startActivity(intent);
    }

    @Override
    public void onResume() {
        super.onResume();
        L.e("MyFragment -================ Resume");
        initData();
    }


    /**
     * 更改头像
     */
    private void updateFaceImg(){
        L.e("updateFaceImg===================");

    }

    /**
     * 更改用户名
     */
    private void updateUsername(){
        L.e("updateUsername===================");
        Intent intent = new Intent(getActivity(), UpdateUsernameActivity.class);
        startActivity(intent);
    }

    /**
     * 更改手机号
     */
    private void updatePhone(){
        L.e("updatePhone===================");
        Intent intent = new Intent(getActivity(), UpdatePhoneActivity.class);
        startActivity(intent);
    }

    /**
     * 更改邮箱
     */
    private void updateEmail(){
        L.e("updateEmail===================");
        Intent intent = new Intent(getActivity(), UpdateEmailActivity.class);
        startActivity(intent);
    }

    /**
     * 更改密码
     */
    private void updatePassword(){
        L.e("updatePassword===================");
        Intent intent = new Intent(getActivity(), UpdatePasswordActivity.class);
        startActivity(intent);
    }

    /**
     * logout
     */
    private void logout(){
        Intent intent = new Intent(getActivity(), MainActivity.class);
        UserDB userDB = LitePal.findFirst(UserDB.class);
        userDB.delete();
        startActivity(intent);
    }
}
